package com.tencent.wxcloudrun.service.business.fangdong;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tencent.wxcloudrun.config.UserContext;
import com.tencent.wxcloudrun.pojo.dto.resp.GetHeTongTenantResp;
import com.tencent.wxcloudrun.pojo.dto.resp.GetHomeListResp;
import com.tencent.wxcloudrun.pojo.dto.resp.HistoryContractResp;
import com.tencent.wxcloudrun.pojo.po.XContractManage;
import com.tencent.wxcloudrun.pojo.po.XContractTemplate;
import com.tencent.wxcloudrun.pojo.po.XHelpmateInfo;
import com.tencent.wxcloudrun.service.XContractManageService;
import com.tencent.wxcloudrun.service.XContractTemplateService;
import com.tencent.wxcloudrun.service.XHelpmateInfoService;
import com.tencent.wxcloudrun.util.WinXinUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContractService {

    @Resource
    private XContractTemplateService xContractTemplateService;

    @Resource
    private WinXinUtils winXinUtils;

    @Resource
    private HouseManageService houseManageService;

    @Resource
    private XHelpmateInfoService xHelpmateInfoService;

    @Resource
    private XContractManageService xContractManageService;

    public String getTmpList() {
        // 获取所有模板
        List<XContractTemplate> templateList = xContractTemplateService.list();
        return templateList.get(0).getTmpId();
    }

    public String preview(String tmpId) {
        LambdaQueryWrapper<XContractTemplate> xContractTemplateLambdaQueryWrapper = new LambdaQueryWrapper<>();
        xContractTemplateLambdaQueryWrapper.eq(XContractTemplate::getTmpId, tmpId);
        XContractTemplate oneByTmpId = xContractTemplateService.getOne(xContractTemplateLambdaQueryWrapper);
        return winXinUtils.batchdownloadfile(oneByTmpId.getFileId());
    }

    public List<HistoryContractResp> getHistoryContractList() {
        String openId = UserContext.getOpenId();
        String landlordId;
        // 如果当前用户是助理，就要找到其对应的房东的ID
        LambdaQueryWrapper<XHelpmateInfo> xHelpmateInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        xHelpmateInfoLambdaQueryWrapper.eq(XHelpmateInfo::getHelpmateId, openId);
        XHelpmateInfo xHelpmateInfoServiceOne = xHelpmateInfoService.getOne(xHelpmateInfoLambdaQueryWrapper);
        if (ObjectUtil.isNotNull(xHelpmateInfoServiceOne) && StrUtil.isNotBlank(xHelpmateInfoServiceOne.getLandlordId())) {
            // 表明当前用户是助理
            landlordId = xHelpmateInfoServiceOne.getLandlordId();
        } else {
            // 当前用户是房东的情况
            landlordId = openId;
        }
        List<GetHomeListResp> homeList = houseManageService.getHomeList();
        LambdaQueryWrapper<XContractManage> xContractManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        xContractManageLambdaQueryWrapper.eq(XContractManage::getLandId, landlordId);
        List<XContractManage> xContractManageList = xContractManageService.list(xContractManageLambdaQueryWrapper);
        List<HistoryContractResp> respList = new ArrayList<>();
        for (GetHomeListResp getHomeListResp : homeList) {
            HistoryContractResp historyContractResp = new HistoryContractResp();
            historyContractResp.setHouseName(getHomeListResp.getHouseName());
            historyContractResp.setHouseId(getHomeListResp.getHouseId());
            List<XContractManage> manageList = xContractManageList.stream().filter(var -> var.getHouseId().equals(getHomeListResp.getHouseId())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(manageList)) {
                historyContractResp.setContractNum(manageList.size());
                List<HistoryContractResp.Contract> contractList = new ArrayList<>();
                for (XContractManage manage : manageList) {
                    HistoryContractResp.Contract contract = new HistoryContractResp.Contract();
                    contract.setContractId(manage.getContractId());
                    contract.setContractDesc("到期时间：" + DateUtil.format(manage.getDeadDate(), "yyyy-MM-dd"));
                    contractList.add(contract);
                }
                historyContractResp.setContractList(contractList);
            }
            respList.add(historyContractResp);
        }
        return respList;
    }

    public String getHeTongJiUrl(String contractId) {
        LambdaQueryWrapper<XContractManage> xContractManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        xContractManageLambdaQueryWrapper.eq(XContractManage::getContractId, contractId);
        List<XContractManage> xContractManageList = xContractManageService.list(xContractManageLambdaQueryWrapper);
        String fileId = xContractManageList.get(0).getFileId();
        return winXinUtils.batchdownloadfile(fileId);
    }

    public List<GetHeTongTenantResp> getHeTongTenant() {
        List<GetHeTongTenantResp> respList = new ArrayList<>();
        String openId = UserContext.getOpenId();
        LambdaQueryWrapper<XContractManage> xContractManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        xContractManageLambdaQueryWrapper.eq(XContractManage::getTenantId, openId);
        xContractManageLambdaQueryWrapper.orderByDesc(XContractManage::getDeadDate);
        List<XContractManage> xContractManageList = xContractManageService.list(xContractManageLambdaQueryWrapper);
        if (CollUtil.isEmpty(xContractManageList)) {
            return respList;
        }
        for (XContractManage contractManage : xContractManageList) {
            GetHeTongTenantResp getHeTongTenantResp = new GetHeTongTenantResp();
            getHeTongTenantResp.setContractId(contractManage.getContractId());
            getHeTongTenantResp.setContractDesc("到期时间：" + DateUtil.format(contractManage.getDeadDate(), "yyyy-MM-dd"));
            respList.add(getHeTongTenantResp);
        }
        return respList;
    }
}
